<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\Comment;
use App\Models\Reply;

class ReplyController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //
        $count = $request -> input('count',10);
        $search = $request -> input('search','');
        $request = $request -> all();
        $comment = Comment::where('gccontent','like','%'.$search.'%')->orderBy('gctime','desc')->paginate($count);
        foreach ($comment as $key => $value) {
            $comment[$key]['reply'] = Reply::where('r_gcid',$value['gcid'])->first();
        }
        return view('admin.reply.index',['title'=>'评论列表','comment'=>$comment,'request'=>$request]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
        $comment = Comment::find($id);
        $reply = Reply::where('r_gcid',$id)->first();
        // dd($reply);
        return view('admin.reply.show',['title'=>'回复查看','comment'=>$comment,'reply'=>$reply]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $comment = Comment::find($id);
        return view('admin.reply.edit',['title'=>'回复','comment'=>$comment]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        $aid = session('data')->aid;
        $reply = new Reply;
        $reply->r_gcid = $id;
        $reply->r_aid = $aid;
        $reply->rcontent = $request->all()['rcontent'];
        $reply->rtime = date('Y-m-d H:i:s');
        if($reply->save()){
            return redirect('/admin/reply')->with('success','回复成功');
        }else{
            return back()->with('error','回复失败');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
